<template>
	<view>
		<uni-status-bar></uni-status-bar>
		<view class="logo_box">
			SS
		</view>
		<view class="title_box">
			欢迎使用家庭药箱
		</view>
		<u-form labelPosition="top" :model="form" ref="form">
			<view class="card_box">
				<u-form-item prop="username" label="账号">
					<u-input v-model="form.username"   placeholder="请输入账号"
						:placeholder-style="placeholderStyle"></u-input>
				</u-form-item>
			</view>
			<view class="card_box">
				<view class="card_box-title">密码</view>
				<u-form-item prop="password">
					<u-input v-model="form.password" type="password" placeholder="请输入密码"
						:placeholder-style="placeholderStyle" />
				</u-form-item>
			</view>
			<view class="card_box">
				<view class="card_box-title">验证码</view>
				<u-form-item prop="verifyCode">
					<u-input v-model="form.verifyCode" placeholder="请输入验证码"
						:placeholder-style="placeholderStyle" />
						<image :src="captcha.data" class="svg-image"></image>
				</u-form-item>
			</view>
		</u-form>
		<view class="card_box card_text_login" @click="login()">
			登录
		</view>
		<view class="u-flex car">
			<view class="color-c7" @click="navigateTo('/pages/forget/forget')">
				忘记密码？
			</view>
			<view class="ma-l-a" @click="navigateTo('/pages/register/register')">
				注册账号
			</view>
		</view>
		<view class="bottom_box">
			<u-divider color="#000000" fontSize="28" margin-top="210" margin-bottom="60" half-width="300"
				border-color="#BBBBBB">其他登录方式</u-divider>
			<image class="icon-vx" src="../../static/icon/wechat_logo.png" mode=""></image>
		</view>
	</view>
</template>

<script>
	import * as auth from '@/api/auth'
	import {svgToBase64} from '@/utils'
	export default {
		data() {
			return {
				placeholderStyle: 'color:#aaaaaa',
				form: {
					username: '',
					password: '',
					verifyCode:''
				},
				captcha:{
					data:'',
					captchaId:''
				},
				rules: {
					username: [{
						required: true,
						message: '请输入账号',
						trigger: 'blur',
					}],
					password: [{
						required: true,
						message: '请输入密码',
						trigger: 'blur',
					}],
					verifyCode: [{
						required: true,
						message: '请输入验证码',
						trigger: 'blur',
					}],
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules);
		},
		mounted() {
			this.getCaptcha()
		},
		methods: {
			getCaptcha(){
				auth.captcha({height:40,with:150,color:'#2c3142'}).then(res=>{
					this.captcha = res.data
					this.captcha.data ='data:image/svg+xml;base64,'+ svgToBase64(res.data.data);
					
				})
			},
			
			login() {
				this.$refs.form.validate(valid => {
					if (valid) {
						uni.showLoading({
							title: '正在登录...'
						});
						this.form.captchaId = this.captcha.captchaId
						auth.login(this.form).then(res => {
							uni.setStorageSync('token', res.data.token)
							uni.hideLoading();
							// uni.setStorageSync('userInfo', JSON.stringify(res.data))
							this.showToast('登录成功', 'success')
							this.reLaunch('/pages/index/index')
						})
					} else {
						return false
					}
				})

			},
			//app第三方登录
			handleThirdLoginApp() {
				const that = this
				uni.getProvider({
					service: 'oauth',
					success: function(res) {
						console.log(res.provider);
						//支持微信、qq和微博等
						if (~res.provider.indexOf('weixin')) {
							uni.login({
								provider: 'weixin',
								success: function(loginRes) {
									console.log("App微信获取用户信息成功", loginRes);
									that.getApploginData(loginRes) //请求登录接口方法
								},
								fail: function(res) {
									console.log("App微信获取用户信息失败", res);
								}
							})
						}
					}
				});
			},
			getApploginData(data) {
				// const data = {
				// 	optionId: data.authResult.openid,
				// 	phone: '',
				// 	photo: ''
				// }
				uni.request({
					url: "https://api.weixin.qq.com/sns/userinfo?access_token=" + data.authResult.access_token +
						"&openid=" + data.authResult.openid,
					method: 'GET',
					dataType: 'json',
					header: {
						'content-type': 'application/x-www-form-urlencoded' // 默认值
					},
					success(res) {}


				})
			}
		}
	}
</script>

<style>
	.logo_box {
		width: 148rpx;
		height: 74rpx;
		margin-top: 102rpx;
		margin-left: 60rpx;
		border-radius: 8rpx;
		text-align: center;
		line-height: 74rpx;
		font-size: 42rpx;
		font-weight: bold;
		color: #FFFFFF;
		background-color: #014AF8;
	}
.svg-image{
	width: 200rpx;
	height: 60rpx;
}
	.title_box {
		font-size: 42rpx;
		margin-top: 24rpx;
		margin-left: 60rpx;
		font-weight: bold;
		/* padding: 140rpx 0 60rpx 0; */
	}

	.card_box {
		margin: 0 60rpx;
		padding: 10rpx 30rpx;
	}

	.card_box-title {
		font-size: 28rpx;
		color: #000000;
	}

	/* .card_box+.card_box {
		margin-top: 36rpx;
	} */

	.card_text_login {
		background-color: #014AF8;
		color: #FFFFFF;
		font-size: 30rpx;
		text-align: center;
		padding-top: 22rpx;
		padding-bottom: 22rpx;
		margin-top: 18rpx;
		border-radius: 8rpx;
	}

	.car {
		color: #014AF8;
		margin: 30rpx 66rpx;
	}

	.ma-l-a {
		margin-left: auto;
	}

	.icon-vx {
		width: 60rpx;
		height: 60rpx;
	}

	.bottom_box {
		margin: 0 36rpx;
		text-align: center;
	}

	.m-a-t {
		margin-top: 60rpx;
	}

	.color-c7 {
		color: #C7C7CC;
	}
</style>
